package eecs285.proj4.gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

@SuppressWarnings("serial")
public class EECSAnimator extends JPanel
{
  protected ImageIcon images[];
  private final int NUM_IMAGES;
  int currentImage = 0;
  private static final int DELAY = 50; // milliseconds
  private static final int NUM_ITERATIONS = 6;
  private int width;
  private int height;
  private int counter;
  Graphics graphics;

  private Timer animationTimer;

  // ---------------------------------------------------------------------------

  public EECSAnimator(String[] fileNames)
  {
    animationTimer = new Timer(DELAY, new TimerHandler());
    NUM_IMAGES = fileNames.length;
    images = new ImageIcon[NUM_IMAGES];
    for (int i = 0; i < NUM_IMAGES; i++)
    {
      images[i] = new ImageIcon(getClass().getResource(fileNames[i]));
    }

    width = images[0].getIconWidth();
    height = images[0].getIconHeight();
    counter = 0;
  } // ctor


  // ---------------------------------------------------------------------------

  public void paintComponent(Graphics g)
  {
    super.paintComponent(g); // call superclass paintComponent
    
    graphics = g;
    
    images[currentImage].paintIcon(this, g, 0, 0);

    // set next image to be drawn only if Timer is running
    if( animationTimer.isRunning() )
    {
      currentImage = (currentImage + 1) % NUM_IMAGES;
    }
    
    counter = counter + 1;
    
    //Make sure last image is green and Stop after N flashes
    if(counter > (NUM_ITERATIONS*NUM_IMAGES))
    {
      
      animationTimer.stop();
      currentImage = 0;
      repaint();
    }
    
  } // end method paintComponent


  // ---------------------------------------------------------------------------

  // start animation, or restart if window is redisplayed
  public void startAnimation()
  {
      counter=0;
      if( animationTimer == null )
      {
        currentImage = 0; // display first image

      } // end if
      else
        // animationTimer already exists, restart animation
      {
        if( !animationTimer.isRunning() )
          animationTimer.restart();
        

      } // else
  } // startAnimation


  // ---------------------------------------------------------------------------

  // stop animation Timer
  public void stopAnimation()
  {
      animationTimer.stop();
  } // end method stopAnimation


  // ---------------------------------------------------------------------------
  
  // stop animation Timer
  public void paintgreen()
  {
    images[0].paintIcon(this, graphics, 0, 0);
  } // end method stopAnimation


  // ---------------------------------------------------------------------------
  public Dimension getMinimumSize()
  {
    return getPreferredSize();
  }


  // ---------------------------------------------------------------------------
  public Dimension getPreferredSize()
  {
    return new Dimension(width, height);

  }


  // ---------------------------------------------------------------------------
  private class TimerHandler implements ActionListener
  {
    public void actionPerformed(ActionEvent actionEvent)
    {
      repaint();
    } // actionPerformed
  } // TimerHandler


}// EECSAnimator
